//
//  HNickNameVC.m
//  BXlive
//
//  Created by huangzhiwen on 2017/3/22.
//  Copyright © 2017年 cat. All rights reserved.
//

#import "HNickNameVC.h"
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <Masonry/Masonry.h>

#import "V2TIMManager.h"

@interface HNickNameVC ()<UITextFieldDelegate>
@property(nonatomic,strong)UIView *navigationView;
@property (nonatomic,strong) UITextField *nickNameTF;
@property (strong, nonatomic) UILabel *tipLb;
@property(nonatomic,strong)UIButton *uploadBtn;
@end

@implementation HNickNameVC

- (void)getTimeInfo {
    [NewHttpManager nextRenickTimeWithSuccess:^(NSDictionary *jsonDic, BOOL flag, NSMutableArray *models) {
        if (flag) {
            NSDictionary *dataDic = jsonDic[@"data"];
            self.tipLb.text = [NSString stringWithFormat:@"%@",dataDic[@"tip"]];
            if (![dataDic[@"limit"] integerValue]) {
//                [self.nickNameTF becomeFirstResponder];
            }
        }
    } failure:^(NSError *error) {
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self setupNavigationView];
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    
//    self.navigationItem.title = NSLocalizedString(@"昵称", nil);
//
//    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    saveBtn.frame = CGRectMake(0, 0, 40, 40);
//    saveBtn.titleLabel.font = CFont(15);
//    [saveBtn setTitleColor:normalColors forState:UIControlStateNormal];
//    [saveBtn setTitle:NSLocalizedString(@"保存", nil) forState:UIControlStateNormal];
//    [saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    
    [self createUI];
    [self getTimeInfo];
}

-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString( @"修改昵称" , nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
    
    UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_navigationView addSubview:uploadBtn];
    [uploadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.right.mas_equalTo(__ScaleWidth(-12));
        make.height.mas_equalTo(__ScaleWidth(26));
        make.width.mas_equalTo(__ScaleWidth(52));
    }];
    [uploadBtn.layer setMasksToBounds:YES];
    uploadBtn.layer.cornerRadius = __ScaleWidth(13);
    [uploadBtn setTitle:NSLocalizedString(@"保存", nil) forState:BtnNormal];
    [uploadBtn setTitleColor:sl_whiteTextColors forState:BtnNormal];
    [uploadBtn setBackgroundColor:sl_normalColors];
    uploadBtn.titleLabel.font = SLPFFont(__ScaleWidth(14));
    [uploadBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:BtnTouchUpInside];
    self.uploadBtn = uploadBtn;
}



- (void)createUI {
    UIView *backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 54)];
    backgroudView.backgroundColor = sl_subBGColors;
    backgroudView.layer.masksToBounds = YES;
    backgroudView.layer.cornerRadius = 5;
    [self.view addSubview:backgroudView];
    WS(weakSelf);
    [backgroudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(-__ScaleWidth(12));
        make.top.equalTo(weakSelf.navigationView.mas_bottom).offset(__ScaleWidth(12));
        make.height.mas_equalTo(__ScaleWidth(54));
    }];
    self.nickNameTF = [[UITextField alloc]initWithFrame:CGRectMake(__ScaleWidth(15), 0, SCREEN_WIDTH-__ScaleWidth(54), 54)];
    self.nickNameTF.textColor =sl_textColors;
    self.nickNameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入你的昵称", nil) attributes:@{NSForegroundColorAttributeName:sl_textSubColors}];
    self.nickNameTF.font = SLPFFont(__ScaleWidth(14));
    self.nickNameTF.textAlignment = NSTextAlignmentLeft;
    self.nickNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.nickNameTF.text = self.nickName;
    self.nickNameTF.delegate = self;
    [self.nickNameTF addTarget:self action:@selector(nickNameTFTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    [backgroudView addSubview:self.nickNameTF];
    UIButton *tfBtn =[self.nickNameTF valueForKey:@"_clearButton"];
    [tfBtn setImage:CImage(@"login_clear") forState:BtnNormal];
    
    _tipLb = [[UILabel alloc]init];
    _tipLb.font = SLPFFont(__ScaleWidth(14));
    _tipLb.textColor = sl_textSubColors;
    _tipLb.numberOfLines = 0;
    [self.view addSubview:_tipLb];
    [_tipLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.equalTo(backgroudView.mas_bottom).offset(__ScaleWidth(10));
    }];
}

-(void)nickNameTFTextDidChange:(UITextField *)tf{
//    NSLog(@"tf:%@",tf.text);
    if (tf.text.length == 0) {
        [self.uploadBtn setTitleColor:sl_textSubColors forState:BtnNormal];
        [self.uploadBtn setBackgroundColor:sl_subBGColors];
    }else{
        [self.uploadBtn setTitleColor:sl_whiteTextColors forState:BtnNormal];
        [self.uploadBtn setBackgroundColor:sl_normalColors];
    }
}

#pragma mark - 保存
- (void)saveBtnClick:(UIButton *)btn {
    if (self.nickNameTF.text.length < 2||self.nickNameTF.text.length > 12) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"昵称2~12个字符", nil)];
    } else{
        [self.nickNameTF resignFirstResponder];
        [[NewHttpRequestHuang sharedNewHttpRequestHuang] userSaveInfoWithNickName:self.nickNameTF.text Gender:@"" Birthday:@"" Province_id:@"" City_id:@"" District_id:@"" Sign:@"" Fields:@"nickname" Success:^(id responseObject) {
            if ([responseObject[@"code"] intValue]==0) {
                BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
                liveUser.nickname = self.nickNameTF.text;
                [BXLiveUser setCurrentBXLiveUser:liveUser];
                if (self.didUpdateNickName) {
                    self.didUpdateNickName(self.nickNameTF.text);
                }
                
                V2TIMUserFullInfo *info = [[V2TIMUserFullInfo alloc] init];
                info.nickName = liveUser.nickname;
                
                [[V2TIMManager sharedInstance] setSelfInfo:info succ:^{
                                        
                                    } fail:^(int code, NSString *desc) {
                                        
                                    }];
                
//                NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//                [dic setValue:@"" forKey:@(NIMUserInfoUpdateTagNick)];
//                [[NIMSDK sharedSDK].userManager updateMyUserInfo:@{@(NIMUserInfoUpdateTagNick): liveUser.nickname} completion:^(NSError * _Nullable error) {
//
//                }];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [BGProgressHUD showInfoWithMessage:responseObject[@"msg"]];
            }
        } Failure:^(NSError *error) {
            [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"修改失败", nil)];
        }];
    }
}

#pragma mark - 隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.nickNameTF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
